package com.zxy.ider.ehome.adapter;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.http.HttpHandler;
import com.zxy.ider.ehome.R;
import com.zxy.ider.ehome.bean.DownloadInfo;
import com.zxy.ider.ehome.bean.MyFile;
import com.zxy.ider.ehome.service.DownloadService;
import com.zxy.ider.ehome.util.BitmapUtil;
import com.zxy.ider.ehome.util.DownloadManager;
import com.zxy.ider.ehome.util.DownloadRequestCallBack;

public class DownloadListAdapter extends BaseAdapter {

	private final Context mContext;
	private static ViewHolder holder = null;
	private LayoutInflater mInflater;

	public static DownloadManager downloadManager;

	public DownloadListAdapter(Context context) {
		mContext = context;
		this.mInflater = LayoutInflater.from(context);
		downloadManager = DownloadService.getDownloadManager(context);
	
	}

	@Override
	public int getCount() {
		if (downloadManager == null)
			return 0;
		Log.i("zxy", "getCount: downloadManager.getDownloadInfoListCount()=="+downloadManager.getDownloadInfoListCount());
		return downloadManager.getDownloadInfoListCount();
	}

	@Override
	public Object getItem(int i) {
		return downloadManager.getDownloadInfo(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {

		DownloadInfo downloadInfo = downloadManager.getDownloadInfo(position);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.downloaditem, null);
			holder = new ViewHolder(downloadInfo, convertView);
			convertView.setTag(holder);
			holder.refresh();
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.update(downloadInfo);
		}
		holder.tv_app_name.setText(downloadInfo.getFileName());
		Glide.with(mContext).load(R.mipmap.file_type_video).into(holder.iv_app_ico);
//		holder.iv_app_ico.setImageBitmap(BitmapUtil.Base64ToBitmap(downloadInfo
//				.getFileIco()));
		HttpHandler<File> handler = downloadInfo.getHandler();
		if (handler != null&&handler.getRequestCallBack()!=null) {
			handler.getRequestCallBack().setUserTag(
					new WeakReference<ViewHolder>(holder));
		}
		return convertView;
	}

	

	

	public class ViewHolder {

		private ImageView iv_app_ico;
		private TextView tv_app_name,tv_app_speed;
		private ProgressBar pb;
		private Button btn_download,btn_cancel;

		private DownloadInfo downloadInfo;

		public ViewHolder(DownloadInfo info, View convertView) {
			this.downloadInfo = info;
			iv_app_ico = (ImageView) convertView.findViewById(R.id.iv_app_ico);
			tv_app_name = (TextView) convertView.findViewById(R.id.tv_app_name);
			pb = (ProgressBar) convertView.findViewById(R.id.pb);
			btn_download = (Button) convertView.findViewById(R.id.btn_download);
			btn_cancel = (Button) convertView.findViewById(R.id.btn_cancel);
			tv_app_speed = (TextView) convertView.findViewById(R.id.tv_app_speed);
			btn_cancel.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
						try {
							Log.i("zxy", "onClick: delete button");
							downloadManager.removeDownload(downloadInfo.getDid());
							DownloadListAdapter.this.notifyDataSetChanged();
						} catch (DbException e) {
							e.printStackTrace();
						}
				}
			});
			btn_download.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if ("打开".equals(btn_download.getText().toString()
							.trim())){
						openFile(new File(downloadInfo.getFileSavePath()));


					} else if ("暂停".equals(btn_download.getText().toString()
							.trim())) {
						btn_download.setText("继续");
						try {
							downloadManager.stopDownload(downloadInfo);
						} catch (DbException e) {
							e.printStackTrace();
						}

					} else if ("继续".equals(btn_download.getText().toString()
							.trim())) {
						btn_download.setText("暂停");
						new Thread(new Runnable() {
							@Override
							public void run() {
								try {
									downloadManager.resumeDownload(
											downloadInfo,
											new DownloadRequestCallBack(mContext,
													downloadManager, downloadInfo
													.getDid()));
								} catch (DbException e) {
									e.printStackTrace();
								}
							}
						}).start();


					}

				}
			});

		}
		private void openFile(File file){
			//Uri uri = Uri.parse("file://"+file.getAbsolutePath());
			Intent intent = new Intent();
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			//设置intent的Action属性
			intent.setAction(Intent.ACTION_VIEW);
			//获取文件file的MIME类型
			String type ="video";
			//设置intent的data和Type属性。
			intent.setPackage("com.droidlogic.videoplayer");
			intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
			//跳转
			mContext.startActivity(intent);
		}




		public void update(DownloadInfo downloadInfo) {
			this.downloadInfo = downloadInfo;
			refresh();
		}

		public void refresh() {
			Log.i("zxy", "refresh: item refresh"+(int) (downloadInfo.getProgress() * 100 / downloadInfo
					.getFileLength()));
			pb.setVisibility(View.VISIBLE);
			if (downloadInfo.getFileLength() > 0) {
				pb.setProgress((int) (downloadInfo.getProgress() * 100 / downloadInfo
						.getFileLength()));
			} else {
				pb.setProgress(0);
			}
			String downloaded = downloadInfo.getProgress()/1024/1024+"MB";
			String totals = downloadInfo.getFileLength()/1024/1024+"MB";
			tv_app_speed.setText(downloaded+"/"+totals);
			String state = downloadInfo.getState();
			if ("SUCCESS".equals(state)) {
				//pb.setVisibility(View.INVISIBLE);
				btn_download.setText("打开");

			} else if ("STOPPED".equals(state)) {

				btn_download.setText("继续");

			}else if("LOADING".equals(state)){
				btn_download.setText("暂停");
			}

		}
	}

}
